package com.lilang.a0608provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class Provider extends ContentProvider {

    static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    private SQLiteCreate sqLiteCreate;
    private SQLiteDatabase database;
    Cursor cursor;
    static {
        matcher.addURI("com.lilang.a0608provider","select_book",1);
        matcher.addURI("com.lilang.a0608provider","insert_book",2);
        matcher.addURI("com.lilang.a0608provider","delete_book",3);
        matcher.addURI("com.lilang.a0608provider","update_book",4);
    }

    @Override
    public boolean onCreate() {
        sqLiteCreate=new SQLiteCreate(getContext());
        database = sqLiteCreate.getWritableDatabase();
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        int Uricode = matcher.match(uri);
        if (Uricode==1){
            Log.e("Main", "query: 执行查询");
            cursor = database.query("book", projection, selection, selectionArgs, null, null, null);
        }else{
            throw  new RuntimeException("数据异常");
        }
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int Uricode = matcher.match(uri);
        if (Uricode==2){
            Log.e("Main", "query: 执行新增");
            database.insert("book",null,values);
        }else{
            throw  new RuntimeException("数据异常");
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int Uricode = matcher.match(uri);
        if (Uricode==3){
            Log.e("Main", "query: 执行删除");
            database.delete("book",selection,selectionArgs);
        }else{
            throw  new RuntimeException("数据异常");
        }
        return 1;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int Uricode = matcher.match(uri);
        if (Uricode==4){
            Log.e("Main", "query: 执行修改");
            database.update("book",values,selection,selectionArgs);
        }else{
            throw  new RuntimeException("数据异常");
        }
        return 1;
    }
}
